package com.smarth.zhizulian.popupwindow;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.adapter.CategoryAdapter;
import com.smarth.zhizulian.bean.Category;

import java.util.List;

/**
 * Created by 18865 on 2017/12/22.
 */

public class PWCategory extends PopupWindow {
    private Context mContext;
    private List<Category> mCategories;
    private Category mWhich;
    private OnResultListener mOnResultListener;

    public PWCategory(Context context, List<Category> categories, Category which) {
        this.mContext = context;
        this.mCategories = categories;
        this.mWhich = which;
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.pw_category, null);
        GridView gv_gridview = (GridView) contentView.findViewById(R.id.gv_gridview);
        ImageView iv_close = (ImageView) contentView.findViewById(R.id.iv_close);

        CategoryAdapter adapter = new CategoryAdapter(mContext, mCategories, mWhich);
        gv_gridview.setAdapter(adapter);

        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PWCategory.this.dismiss();
            }
        });

        gv_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (mOnResultListener != null) {
                    mOnResultListener.onResult(mCategories.get(i));
                }
                PWCategory.this.dismiss();
            }
        });

//        gv_gridview.setAdapter(adapter);
        // 设置SelectPicPopupWindow的View
        this.setContentView(contentView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
//        // 刷新状态
//        this.update();
//        //实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0xb0000000);
//        //设置SelectPicPopupWindow弹出窗体的背景
//        this.setBackgroundDrawable(dw);
//        this.setOnDismissListener(new OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                backgroundAlpha(1f);
//            }
//        });
    }

    public interface OnResultListener {
        public void onResult(Category category);
    }

    public void setOnResultListener(OnResultListener OnResultListener) {
        this.mOnResultListener = OnResultListener;
    }
}
